﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

using Common.Base;

using CommunicationLibrary.TransportData;

using GameCommon.Manager;
using GameCommon.ManagerInterfaces;

using GameGraphics.Helper;

using GameNetwork.Network;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameGraphics.Components
{
    [DebuggerDisplay("Route")]
    internal class Route : Texture
    {
        private IEnumerable<KeyValuePair<Unit, float>> units;

        public Route(string id, Texture2D texture, RouteTypeEnum routeType, BoardTile startTile, BoardTile endPosition)
            : base(id, texture)
        {
            this.StartArrayPosition = startTile.GridPosition;
            this.EndArrayPosition = endPosition.GridPosition;
            this.EndPosition = endPosition.TileCenterPosition;
            this.StartPosition = startTile.TileCenterPosition;
            this.units = new Dictionary<Unit, float>();

            this.Color = routeType == RouteTypeEnum.Militia ? Color.Red : Color.Blue;
            this.Layer = 98;
        }

        public void Update(IEnumerable<KeyValuePair<Unit, float>> route)
        {
            this.units = route;
        }

        public override void Draw(IDrawingManager manager, GameTime gameTime)
        {
            base.Draw(manager, gameTime);

            foreach (var unit in this.units)
            {
                var tex = Managers.ContentManager.GetBoardTexture2D("Units/Unit");
                var pos = UnitPositionHelper.GetPosition(this.StartPosition, this.EndPosition, unit.Value);
                var dim = new Dimension(pos, 3, 3);
                manager.Add(tex, dim.Rectangle, Color.White, 100);
            }
        }

        public Position StartArrayPosition { get; private set; }

        public Position EndArrayPosition { get; private set; }

        public Position StartPosition { get; private set; }

        public Position EndPosition { get; private set; }

        public override string ToString()
        {
            return string.Format("Route {0}", this.Id);
        }
    }
}